package cn.mxb.logic.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

import cn.mxb.floor.pagination.Result;
import cn.mxb.floor.web.BaseAction;
import cn.mxb.logic.entity.SysCount;
import cn.mxb.logic.entity.SysTown;
import cn.mxb.logic.entity.SysVillage;
import cn.mxb.logic.entity.Organization;
import cn.mxb.logic.service.ISysCountService;
import cn.mxb.logic.service.ISysTownService;
import cn.mxb.logic.service.ISysVillageService;
import cn.mxb.logic.service.IOrganizationService;

@SuppressWarnings({ "serial", "rawtypes" })
public class OrganizationAction extends BaseAction implements Preparable,ModelDriven {
	protected static final String List_JSP = "/WEB-INF/page/web/floor/organizationList.jsp";
	protected static final String ORGANIZATIONCOUNTADD_JSP = "/WEB-INF/page/web/floor/organizationCountAdd.jsp";
	protected static final String ORGANIZATIONTOWNADD_JSP = "/WEB-INF/page/web/floor/organizationTownAdd.jsp";
	protected static final String UPDATE_JSP = "/WEB-INF/page/web/floor/organizationUpdate.jsp";
	
	private Organization organization;
	@Autowired
	private IOrganizationService organizationService;
	@Autowired
	private ISysCountService sysCountService;
	@Autowired
	private ISysTownService sysTownService;
	@Autowired
	private ISysVillageService sysVillageService;
	
	private Result<Organization> pageResult;
	
	private List<Organization> organizationAllList; 
	private List<SysCount> sysCountAllList; 
	private List<SysTown> sysTownAllList; 
	private List<SysVillage> sysVillageAllList; 
	
	private String countIdY;
	private String townIdY;
	
	private Map<Integer,String> countNameMap = new HashMap<Integer, String>();  //县名
	private Map<Integer,String> townNameMap = new HashMap<Integer, String>();  //镇名
	private Map<String,String> villageNameMap = new HashMap<String, String>();  //村名
	
	/**
	 * @return_type: String
	 * @file_name: 跳转添加界面
	 */
	public String openAdd(){
		
		
		sysVillageAllList = sysVillageService.findAllList();
		if(organization.getOrganizationLevel() == 1){
			sysCountAllList = sysCountService.findAllList();
			setForwardView(ORGANIZATIONCOUNTADD_JSP);
			return SUCCESS;
		}
		if(organization.getOrganizationLevel() == 2){
			sysTownAllList = sysTownService.findAllList();
			setForwardView(ORGANIZATIONTOWNADD_JSP);
			return SUCCESS;
		}
		
		return SUCCESS;
	}
	
	/**
	 * 添加
	 * @return_type: String
	 */
	public String add(){
		if(countIdY !=null && !"".equals(countIdY)){
			System.out.println("县级添加"+countIdY);
			organization.setAdministrativeId(countIdY);
			organization.setOrganizationLevel(1);
			try{
				organizationService.add(organization);
			}catch(Exception e){
				System.out.println(e);
			}
		}
		if(townIdY !=null && !"".equals(townIdY)){
			System.out.println("镇级添加");
			organization.setAdministrativeId(townIdY);
			organization.setOrganizationLevel(2);
			organizationService.add(organization);
		}
		return openList();		
	}
	
	/**
	 * @file_name 打开列表
	 * @return 
	 */
	public String openList(){
		try{
			pageResult = organizationService.find(organization, getPage(), getRow());
			setForwardView(List_JSP);
			return SUCCESS;
		}catch(Exception e){
			System.out.println(e);
		}
		return SUCCESS;
	}
	
	/**
	 * @file_name:删除 
	 */
	public String delete(){
		organizationService.delete(organization.getId());
		return openList();
	}
	
	/**
	 * @return_type: 
	 * @file_name:  打开修改页面
	 */
	public String openUpdate(){
		organization =  organizationService.findByProperty("id",organization.getId()+"");
		sysCountAllList = sysCountService.findAllList();
		sysTownAllList = sysTownService.findAllList();
		sysVillageAllList = sysVillageService.findAllList();
		setForwardView(UPDATE_JSP);
		return SUCCESS;
		
	}
	
	/**
	 * 修改
	 * @return_type: 
	 */
	public String update(){
		organizationService.add(organization);
		organization = null;
		return openList();
	}
	
	
	@Override
	public Object getModel() {
		return organization;
	}
	@Override
	public void prepare() throws Exception {
		if(organization ==null){
			organization = new Organization();
		}
	}


	public Organization getOrganization() {
		return organization;
	}

	public void setOrganization(Organization organization) {
		this.organization = organization;
	}

	public Result<Organization> getPageResult() {
		return pageResult;
	}

	public void setPageResult(Result<Organization> pageResult) {
		this.pageResult = pageResult;
	}
	
	public List<SysCount> getSysCountAllList() {
		return sysCountAllList;
	}

	public void setSysCountAllList(List<SysCount> sysCountAllList) {
		this.sysCountAllList = sysCountAllList;
	}

	public List<SysTown> getSysTownAllList() {
		return sysTownAllList;
	}

	public void setSysTownAllList(List<SysTown> sysTownAllList) {
		this.sysTownAllList = sysTownAllList;
	}

	public Map<Integer, String> getCountNameMap() {
		return countNameMap;
	}

	public void setCountNameMap(Map<Integer, String> countNameMap) {
		this.countNameMap = countNameMap;
	}

	public Map<Integer, String> getTownNameMap() {
		return townNameMap;
	}

	public void setTownNameMap(Map<Integer, String> townNameMap) {
		this.townNameMap = townNameMap;
	}

	

	public Map<String, String> getVillageNameMap() {
		return villageNameMap;
	}

	public void setVillageNameMap(Map<String, String> villageNameMap) {
		this.villageNameMap = villageNameMap;
	}

	public List<SysVillage> getSysVillageAllList() {
		return sysVillageAllList;
	}

	public void setSysVillageAllList(List<SysVillage> sysVillageAllList) {
		this.sysVillageAllList = sysVillageAllList;
	}

	public List<Organization> getOrganizationAllList() {
		return organizationAllList;
	}

	public void setOrganizationAllList(List<Organization> organizationAllList) {
		this.organizationAllList = organizationAllList;
	}

	public String getCountIdY() {
		return countIdY;
	}

	public void setCountIdY(String countIdY) {
		this.countIdY = countIdY;
	}

	public String getTownIdY() {
		return townIdY;
	}

	public void setTownIdY(String townIdY) {
		this.townIdY = townIdY;
	}


	

	
	
}
